
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;
using Swaf.Gui;

namespace Syrinx.Gui
{
	public class CaptchaVerifierBizProc : BaseBusinessProcess
	{
		public static string SessionKey = "LastCaptchaKey";
		protected bool m_failedVerification = false;

		public CaptchaVerifierBizProc(ActiveBizProc procState)
			: base(procState)
		{
			m_currentBizObj = createNewBizObj();
			m_retrievedCurrentBizObj = true;
		}

		protected void resetCaptchaKey()
		{
			IBizObj clientInfo = App.bizObjMgr.create("UserVerifyClientInfo"); //TODO: Need to populate this bizobj with values about the client like ip, browser info, etc.
			clientInfo.put("privilegeName", "AppFunctions.ManageForumPosts");
			string usage = (string)m_procState.get("usage");
			string key = null;
			if (!isHiddenForEdit(usage))
				clientInfo.put("canEdit", true);
			IResults rc = App.ruleMgr.execute("shouldShowUserVerification", usage, clientInfo);
			key = rc.CallResults as string;
			
			//List<IBizObjDisplay> detailDisplays = m_procState.getDetailsDisplays();
			//if (detailDisplays != null)
			//    foreach (IBizObjDisplay disp in detailDisplays)
			//        disp.Visible = key != null;

			State.State.put(SessionKey, key);
			App.sessionData.put(CaptchaVerifierBizProc.SessionKey, key);
		}

		protected bool isHiddenForEdit(string usage)
		{
			bool isit = false;
			if (string.Compare(usage, "canedit", true) == 0 && !m_failedVerification)
			{
				ActiveBizProc p = m_procState.getParentBizProc("pid");
				isit = !p.BizProc.CanEdit;
			}
			return isit;
		}

		public override void showData(ForceLevel level)
		{
			Syrinx.RandomStringGenerator x = new RandomStringGenerator(3,4,null, false);
			m_currentBizObj.put("imageName", x.generateRandomString());
			resetCaptchaKey();
			m_procState.ForceShowBizObj = true;
			base.showData(level);
		}

		protected override void setupRuleNames()
		{
		}

		public override Privileges CurrentEditPrivilege
		{
			get 
			{
				string key = (string)State.State.get(SessionKey);
				return key==null?Privileges.ReadOnly : Privileges.Edit; 
			}
		}

		public bool verifyUser()
		{
			string key = (string)State.State.get(SessionKey);
			if (key == null)
				return true;

			bool verified = false;
			updateBizObj();
			string userInfo = (string)m_currentBizObj.get("captchaInput");
			if (userInfo != key)
			{
				m_failedVerification = true;
				m_procState.BPC.Messenger.add("Error-CaptchaResponseWrong", null);
				//TODO: Log failure

			}
			else
				verified = true;
			m_currentBizObj.put("captchaInput", "");
			return verified;
		}

		protected override string BizObjTypeName
		{
			get
			{
				return "UserVerification";
			}
		}

		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("VerifyUser", true, true, verifyUser, this));
		}

		public override void clearRetrievedData()
		{
			//intentionally left blank as default behavior is inapropriate for this class.
		}
	}
}
